import { useEffect } from "react";
import { Row, Col, Pagination } from "antd";
import { Card as SemiCard, List, ButtonGroup, Button } from "@douyinfe/semi-ui";
import { IconPlusCircle, IconRefresh, IconEdit } from "@douyinfe/semi-icons";
import { useModel } from "umi";
import { useReactive } from "ahooks";

import useGatewayType from "../../hooks/resource/useGatewayType";

import ApplicationDetail from "../../components/organization_application/Detail";

const OrganizationApplicationByGatewayType = () => {
    const { windowInnerHeight } = useModel("global");
    const { dataLoading, loadGatewayTypes, gatewayTypes } = useGatewayType();
    const { dataLoading: appLoading, applications, totalCount, loadApplications } = useGatewayType();

    const state = useReactive({
        selectedGatewayType: {},
        selectedApplication: {},
        page: 1,
        pageSize: 20,
    });

    useEffect(() => {
        loadGatewayTypes();
    }, []);

    useEffect(() => {
        if (gatewayTypes?.length > 0) {
            let selectedGatewayType = gatewayTypes.find((item) => item.name === state.selectedGatewayType?.name);
            !selectedGatewayType && (selectedGatewayType = gatewayTypes[0]);
            state.selectedGatewayType = selectedGatewayType;
        }
    }, [gatewayTypes]);

    const loadGatewayTypeApplications = () => {
        loadApplications({
            typeName: state.selectedGatewayType.name,
            pageSize: state.pageSize,
            page: state.page,
        });
    };

    useEffect(() => {
        if (state.selectedGatewayType?.name) {
            loadGatewayTypeApplications();
        }
    }, [state.selectedGatewayType]);

    useEffect(() => {
        if (applications?.length > 0) {
            let selectedApplication = applications.find((item) => item.appUuid === state.selectedApplication?.appUuid);
            !selectedApplication && (selectedApplication = applications[0]);
            state.selectedApplication = selectedApplication;
        } else {
            state.selectedApplication = {};
        }
    }, [applications]);

    const cardBodyHeight = windowInnerHeight - 90 - 53 - 20;

    return (
        <Row gutter={10} style={{ display: "flex", padding: 10 }}>
            <Col span={3}>
                <SemiCard
                    title={<span style={{ lineHeight: "32px", fontSize: 16 }}>网关类型</span>}
                    headerExtraContent={
                        <ButtonGroup>
                            <Button icon={<IconRefresh />} onClick={loadGatewayTypes}></Button>
                        </ButtonGroup>
                    }
                    headerStyle={{ padding: "10px 14px" }}
                    shadows="always"
                    bordered={false}
                    bodyStyle={{ padding: 1, height: cardBodyHeight, overflow: "auto" }}
                >
                    <List
                        loading={dataLoading}
                        dataSource={gatewayTypes}
                        renderItem={(item, index) => (
                            <List.Item
                                key={index}
                                className={`list-item ${state.selectedGatewayType?.name === item?.name ? "list-selected-item" : ""}`}
                                main={<div title={item?.notes}>{item?.name || "-"}</div>}
                                onClick={() => (state.selectedGatewayType = item)}
                            ></List.Item>
                        )}
                    />
                </SemiCard>
            </Col>
            <Col span={5}>
                <SemiCard
                    title={<span style={{ lineHeight: "32px", fontSize: 16 }}>应用列表</span>}
                    headerExtraContent={
                        <ButtonGroup>{state.selectedGatewayType?.name && <Button icon={<IconRefresh />} onClick={loadGatewayTypeApplications}></Button>}</ButtonGroup>
                    }
                    headerStyle={{ padding: "10px 14px" }}
                    shadows="always"
                    bordered={false}
                    bodyStyle={{ padding: 1, height: cardBodyHeight - 48, overflow: "auto" }}
                    footerStyle={{ padding: 0 }}
                    footerLine={true}
                    footer={
                        <div style={{ padding: "11px 0", textAlign: "center" }}>
                            <Pagination
                                simple
                                total={totalCount}
                                pageSize={state.pageSize}
                                position={["none", "bottomCenter"]}
                                hideOnSinglePage={false}
                                showSizeChanger={false}
                                onChange={(page) => (state.page = page)}
                            />
                        </div>
                    }
                >
                    <List
                        loading={appLoading}
                        dataSource={applications}
                        renderItem={(item, index) => (
                            <List.Item
                                key={index}
                                className={`list-item ${state.selectedApplication?.appUuid === item?.appUuid ? "list-selected-item" : ""}`}
                                main={<div title={item.appDesc}>{item?.appName || "-"}</div>}
                                onClick={() => (state.selectedApplication = item)}
                            ></List.Item>
                        )}
                    />
                </SemiCard>
            </Col>
            <Col span={16}>
                <SemiCard
                    title={<span style={{ lineHeight: "32px", fontSize: 16 }}>应用信息 - {state.selectedApplication?.appName}</span>}
                    headerExtraContent={<ButtonGroup></ButtonGroup>}
                    headerStyle={{ padding: "10px 14px" }}
                    shadows="always"
                    bordered={false}
                    bodyStyle={{ padding: 10, height: cardBodyHeight }}
                >
                    {state.selectedApplication?.appUuid && <ApplicationDetail item={state.selectedApplication || {}} reloadData={loadGatewayTypeApplications} />}
                </SemiCard>
            </Col>
        </Row>
    );
};

export default OrganizationApplicationByGatewayType;
